package org.zh.home.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;
import org.zh.home.dao.BaseDao;
import org.zh.home.dao.IUserDao;
import org.zh.home.entity.Role;
import org.zh.home.entity.User;
import org.zh.home.model.Pager;
import org.zh.home.model.Param;
import org.zh.home.model.Query;
import org.zh.home.utils.QueryUtils;

@Repository
public class UserDao extends BaseDao<User> implements IUserDao {

	@Override
	public List<Role> listRoleByUser(int userId) {
		String sql = "select r.* from sdf_role r join sdf_user_role ur on(r.id = ur.role_id) where ur.user_id = "+userId;
		return super.listBySql(sql, Role.class, true);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Integer> listRoleIdsByUser(int userId) {
		String sql = "select ur.role_id from sdf_user_role ur where ur.user_id = "+userId;
		return super.getSession().createQuery(sql).list();
	}

	@Override
	public Pager<User> findUser(List<Query> model) {
		Param param = QueryUtils.generateHQLBase("from User", "u", model);
		return super.find(param.getQuery(), param.getValues().toArray());
	}
}
